<template>
  <div class="login-container">
    <div class="logo">
      <img src="../../assets/images/logo.png" alt="" />
    </div>

    <div class="form-box">
      <div class="logo-title">
        <img src="../../assets/images/login_title.png" alt="" />
      </div>
      <el-form
        :model="loginForm"
        :rules="rulesForm"
        ref="loginForm"
        class="demo-loginForm"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            type="password"
            placeholder="请再次输入密码"
            v-model="loginForm.repassword"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="login-btn" @click="btnSubmit"
          >注册</el-button
        >
        <el-link @click="$router.push('/login')">去登录</el-link>
      </el-form>
    </div>
  </div>
</template>
<script>
import { regForm } from '../../api/user.js'
export default {
  name: '',
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('输入不能为空,请再次输入密码'))
      } else if (value !== this.loginForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: '',
        repassword: ''
      },
      rulesForm: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '请输入3-10位用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' }
        ],
        repassword: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  created () {},
  computed: {},
  methods: {
    btnSubmit () {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          const res = await regForm(this.loginForm)
          console.log(res.data, 999)
          console.log(res.data.code)
          if (res.data.code === 0) {
            this.$message({
              message: '恭喜你，注册成功',
              type: 'success'
            })
            this.$router.push('/login')
          } else {
            //  校验不成功
            this.$message({
              message: '注册失败',
              type: 'error'
            })
          }
        } else {
          //  校验不成功
          this.$message({
            message: '注册失败',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>
<style lang='less'  scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  // background-color: pink;
  background: url('../../assets/images/login_bg.jpg') no-repeat center / cover;
  .logo {
    width: 1200px;
    margin: 0 auto;
  }

  .form-box {
    width: 400px;
    height: 330px;
    background-color: #fff;

    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 20px 30px;
    box-sizing: border-box;
    .logo-title {
      text-align: center;
      margin-bottom: 15px;
    }

    .el-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
